package com.example.java_chatroom.api;

import com.example.java_chatroom.model.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Q
 * @create 2025-07-06-17:39
 */
@RestController
public class MessageSessionAPI {
    @Resource
    private MessageSessionMapper messageSessionMapper;
    @Resource
    private MessageMapper messageMapper;
    @GetMapping("/sessionList")
    @ResponseBody
    public Object getMessageSessionList(HttpServletRequest req) {
        List<MessageSession> messageSessionList = new ArrayList<>();
        //1.获取当前userId
        HttpSession session = req.getSession(false);
        if(session == null) {
            System.out.println("[getMessageSessionList]获取不到session对象");
            return messageSessionList;//返回空user
        }
        //2.从会话中获取到用户对象信息
        User user = (User) session.getAttribute("user");
        if(user == null) {
            System.out.println("[getMessageSessionList]获取不到user对象");
            return messageSessionList;//返回空user
        }
        //2.根据userId,查询出相应会话
        List<Integer> sessionIdList = messageSessionMapper.getSessionIdsByUserId(user.getUserId());
        for (int sessionId: sessionIdList) {
            MessageSession messageSession = new MessageSession();
            messageSession.setSessionId(sessionId);
            //3.遍历会话，查询出每个会话涉及的好友
            List<Friend> friends = messageSessionMapper.getFriendsBySessionId(sessionId, user.getUserId());
            messageSession.setFriends(friends);
            //4.遍历会话，查询出涉及好友的最后一条消息
            String lastMessage = messageMapper.getLastMessageBySessionId(sessionId);
            if(lastMessage == null) {
                messageSession.setLastMessage("");
            }else {
                messageSession.setLastMessage(lastMessage);
            }
            messageSessionList.add(messageSession);
        }
        return messageSessionList;
    }
    /**
     * 新增会话
     */
    @PostMapping("/session")
    @ResponseBody
    @Transactional
    public Object addMessageSession(int toUserId, @SessionAttribute("user") User user) {
        HashMap<String,Integer> hashMap = new HashMap<>();
        //进行数据库的插入
        //1.先给message_session表里插入记录，使得到会话Id
        MessageSession messageSession = new MessageSession();
        messageSessionMapper.addMessageSession(messageSession);
        //2.给message_session_user插入记录
        MessageSessionUserItem item1 = new MessageSessionUserItem();
        item1.setSessionId(messageSession.getSessionId());
        item1.setUserId(user.getUserId());
        messageSessionMapper.addMessageSessionUser(item1);
        //3.给message_session_user插入记录
        MessageSessionUserItem item2 = new MessageSessionUserItem();
        item2.setSessionId(messageSession.getSessionId());
        item2.setUserId(toUserId);
        messageSessionMapper.addMessageSessionUser(item2);

        hashMap.put("sessionId",messageSession.getSessionId());
        return hashMap;
    }
}
